<?php
namespace Admin\Controller;
use Think\Controller;
import('Org.Wechat.wxBizMsgCrypt');   //此处import</strong> 
class MenuController extends Controller {
    public function index(){
		if( session('?access_token') ){
			//access_token 过期
			if( time() >= session('access_token_expire') ){
				$_access_token = fnGetAccesToken()["access_token"];
			}else{
				$_access_token = session('access_token');
			}
		}else{
			$_access_token = fnGetAccesToken()["access_token"];
		}
		
		$jsonmenu = '{
			"button":[{
				"type":"view",
				"name":"首页",
				"url":"http://cj0826.top/nhzgh/Menu/baseAuth?url=http://cj0826.top/nhqd/html/index.html"
			},{
				"name":"互动",
				"sub_button":[{
					"type":"view",
					"name":"最新活动",
					"url":"http://m.hao123.com/a/tianqi"
				},{
					"type":"view",
					"name":"正在投票",
					"url":"http://m.hao123.com/a/tianqi"
				},{
					"type":"view",
					"name":"测试",
					"url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('APPID').'&redirect_uri='.urlencode('http://cj0826.top/nhqd/html/index.html').'&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
				}]
			},{
				"type":"view",
				"name":"商城",
				"url":"http://m.hao123.com/a/tianqi"
			}]
		 }';


		$result = https_request("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$_access_token, $jsonmenu);
		var_dump($result);
	}
	
    public function baseAuth(){
		//1.准备scope为snsapi_userInfo网页授权页面  
		$redirecturl = urlencode(I('url'));  
		$snsapi_userInfo_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('APPID').'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';  

		//2.用户手动同意授权,同意之后,获取code  
		//页面跳转至redirect_uri/?code=CODE&state=STATE  
		$code = I('code');  
		if( !isset($code) ){  
			header('Location:'.$snsapi_userInfo_url);  
		}
		  
		//3.通过code换取网页授权access_token  
		$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APPID').'&secret='.C('APPSECRET').'&code='.$code.'&grant_type=authorization_code';  
		$content = _request($curl);  
		$result = json_decode($content);  
		  
		//4.通过access_token和openid拉取用户信息  
		$webAccess_token = $result->access_token;  
		$openid = $result->openid;  
		$userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';  
		  
		$recontent = _request($userInfourl);  
		$userInfo = json_decode($recontent,true);  
		$this->ajaxReturn(array('status' => 1, 'data' => $userInfo, 'msg' => '获取成功'));
	}
	
    public function getOpenId(){
		//3.通过code换取网页授权access_token和openid  
		$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APPID').'&secret='.C('APPSECRET').'&code='.I('code').'&grant_type=authorization_code';  
		$content = _request($curl);  
		$result = json_decode($content,true);  
		$this->ajaxReturn(array('status' => 1, 'data' => $result, 'msg' => '编辑失败'));
	}
	
    public function veriWx(){
		$signature = I('signature');
		$timestamp = I('timestamp');
		$nonce = I('nonce');
		$echostr = I('echostr');
		$token = '123';
		if (! empty ( $echostr) && ! empty ( $signature ) && ! empty ($nonce )) {
		  //定义一个数组，存储其中3个参数，分别是timestamp，nonce和token
		  $tempArr = array($nonce,$timestamp,$token);
		  //进行排序
		  sort($tempArr,SORT_STRING);
		  //将数组转换成字符串
		  $tmpStr = implode($tempArr);
		  //进行sha1加密算法
		  $tmpStr = sha1($tmpStr);
		  //判断请求是否来自微信服务器，对比$tmpStr和$signature
		  if($tmpStr == $signature)
		  {
			echo $echostr;
		  }else{
			  echo false;
		  }
		  exit();
		}  
	}
}
